home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / ve < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.9 KB  |  181 lines

  1. #!/bin/ksh
  2. # @(#) ve.ksh 1.2 96/06/15
  3. # 92/03/06 john h. dubois iii (john@armory.com)
  4. # 92/10/16 exec editor
  5. # 92/11/22 pass -opt and +opt to editor
  6. # 93/04/29 pass + to vi
  7. # 93/11/19 Added -p option
  8. # 94/04/23 Read $HOME/.ve
  9. # 95/05/17 Change window title to reflect files being edited
  10. # 96/06/15 Added d option.
  11. # 96/09/03 Remove cwd from filenames.
  12.  
  13. typeset -i ShowPath=0
  14.  
  15. alias istrue="test 0 -ne"
  16. alias isfalse="test 0 -eq"
  17.  
  18. name=${0##*/}
  19. rcFile=.ve
  20. Usage="Usage: $name [-hdnp] [[-+]vi_option] filename ..."
  21. changeDir=false
  22.  
  23. while getopts :hdnp opt; do
  24.     case $opt in
  25.     h)
  26.     print -u2 -- \
  27. "$name: edit a file in EDITPATH.
  28. $Usage
  29. For each filename given, $name searches each directory named in the
  30. environment variable EDITPATH in the order given and finds the first
  31. instance of filename.  If a file is not found, a message is printed.
  32. When all of the files have been searched for, $name invokes the editor on
  33. them.  EDITPATH has the same format as PATH and CDPATH, except that
  34. directories may be separated by either a ':' or whitespace, and the usual
  35. ksh variable and filename expansions are done.  Examples:
  36. dir1:dir2:dir3:...
  37. dir1{a,b}:dir2/*:dir3:$LIB/foo:~/mail
  38. EDITPATH can also be set in a file in the user's home directory named
  39. \"$rcFile\".  To determine what editor to use, $name first looks at the value
  40. of the environment variable EDITOR, then VISUAL.  If neither is set, vi is
  41. used.  Initial arguments that start with '-' or '+' are passed to the editor
  42. without interpretation.  If vi is used, the argument '+' is passed.
  43. If the environment variables WINTITLE and DISPLAY are set, an xterm escape
  44. sequence is sent to change the window title to the name of the files being
  45. edited, followed by the value of WINTITLE in parentheses.  After editing is
  46. completed, another sequence is sent to change the window title back to
  47. the value of WINTITLE by itself.
  48. The directory that the editor is invoked from is removed from the head of any
  49. filenames that begin with it.
  50. Options:
  51. Some of the options below can be turned on by putting varname=1 in the $rcFile
  52. file, where varname is the variable given in parentheses in the option
  53. description.
  54. -h: print this help.
  55. -d: Change the working directory to the directory that the first file is found
  56.     in before running the editor, so that if the editor is shelled out of the
  57.     shell's working directory will be that directory, and if a new file is
  58.     switched to in the editor the file will be searched for relative to that
  59.     directory.  (CHANGEDIR)
  60. -n: Do not send an xterm title escape sequence.  (NOTITLE)
  61. -p: show what EDITPATH looks like after expansion, then quit."
  62.     exit 0
  63.     ;;
  64.     d) changeDir=true;;
  65.     n) NOTITLE=true;;
  66.     p) ShowPath=1;;
  67.     ?)
  68.     break    # start of vi options
  69.     ;;
  70.     esac
  71. done
  72.  
  73. # remove args that were options
  74. let OPTIND=OPTIND-1
  75. shift $OPTIND
  76.  
  77. editpath=$EDITPATH    # let environment overrule rcfile
  78. rcFile=$HOME/$rcFile
  79. [ -f $rcFile -a -r $rcFile ] && . $rcFile
  80. if [ -z "$editpath" ] ; then
  81.     if [ -z "$EDITPATH" ] ; then
  82.     print -ru2 -- "$name: EDITPATH not set."
  83.     exit 1
  84.     fi
  85.     editpath=$EDITPATH
  86. fi
  87.  
  88. [ -z "$WINTITLE" -o -z "$DISPLAY" -o -n "$NOTITLE" ] && NOTITLE=true ||
  89. NOTITLE=false
  90.  
  91. typeset -i argind=0 numopts i
  92. unset args error
  93. while [[ "$1" = [-+]* ]]; do
  94.     args[argind]=$1
  95.     let argind+=1
  96.     shift
  97. done
  98. numopts=argind
  99.  
  100. set -A files -- "$@"
  101. OIFS=$IFS
  102. IFS=":$IFS"
  103. set -- $editpath
  104. IFS=$OIFS
  105.  
  106. if istrue ShowPath; then
  107.     IFS=:
  108.     print -r -- "$*"
  109.     exit
  110. fi
  111.  
  112. if [ $# -lt 1 ]; then
  113.     print -ru2 "$Usage
  114. Use -h for help."
  115.     exit 1
  116. fi
  117.  
  118. for file in "${files[@]}"; do
  119.     for dir; do
  120.         if [ -f "$dir/$file" ]; then
  121.         args[argind]=$dir/$file
  122.         let argind+=1
  123.         continue 2
  124.     fi
  125.     done
  126.     print -ru2 -- "$name: $file: file not found."
  127.     error=1
  128. done
  129.  
  130. if [ -n "$error" ]; then
  131.     if [ argind -gt numopts ]; then
  132.     print -ru2 -n -- "press return to edit other files..."
  133.     read
  134.     else
  135.     exit
  136.     fi
  137. fi
  138.  
  139. EDITOR=${EDITOR:-${VISUAL:-vi}}
  140. [[ "$EDITOR" = ?(*/)vi ]] && EDITOR="$EDITOR +"
  141.  
  142. function ResetTitle {
  143.     print -n "\033]0;$OWINTITLE\007"
  144. }
  145.  
  146. [ -n "$CHANGEDIR" ] && changeDir=true
  147. if $changeDir; then
  148.     file1=${args[numopts]}
  149.     if [[ "$file1" = */* ]]; then
  150.     dir1=${file1%/*}
  151.     # This should never happen since we already checked for the existance
  152.     # of this file, but check anyway.
  153.     if [ ! -d "$dir1" -o ! -x "$dir1" ]; then
  154.         print -n -ru2 \
  155.     "Cannot change working directory to $dir1.  Press return to continue..."
  156.         read
  157.     else
  158.         cd "$dir1"
  159.     fi
  160.     fi
  161. fi
  162.  
  163. let i=numopts
  164. while [ i -lt argind ]; do
  165.     args[i]=${args[i]#$PWD/}
  166.     let i+=1
  167. done
  168.  
  169. if $NOTITLE; then
  170.     exec $EDITOR "${args[@]}"
  171. else
  172.     OWINTITLE=$WINTITLE
  173.     # Change WINTITLE in environment, so that in case another app that
  174.     # uses WINTITLE is run from the editor, it will use modified value
  175.     WINTITLE="${files[*]} ($OWINTITLE)"
  176.     trap ResetTitle INT QUIT
  177.     print -n "\033]0;$WINTITLE\007"      # set win title
  178.     $EDITOR "${args[@]}"
  179.     ResetTitle
  180. fi
  181.